package com.rao.quartz.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @Author: raoguang
 * @Date: 2022/09/27/2:26
 * @Description:
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(Environment environment) {
        Profiles profiles = Profiles.of("dev");
        //获取生产环境
        //通过environment.acceptsProfiles(profiles)判断自己是否处在指定环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }


    //配置了swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {//该方法返回对象为Docket,并将该对象注入到Bean里面

        Profiles profiles = Profiles.of("dev", "pro");
        //获取生产环境
        //通过environment.acceptsProfiles(profiles)判断自己是否处在指定环境
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)   //是否启动swagger,如果为false,则swagger不能在浏览器中访问
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式.
                //any()表示扫描所有的包
                //none()表示所有包都不扫描
                //withClassAnnotation表示扫描类上面的注解，参数为一个注解的反射对象，值可以为：@GetMapping.class
                //withMethodAnnotation表示扫描方法上面的注解
                .apis(RequestHandlerSelectors.basePackage("com.rao.quartz.controller"))
                //path表示过滤,常用的为ant()
                //.paths(PathSelectors.ant("/rao/**"))
                .build();


    }

    //配置swagger的信息等于apiInfo
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("饶广", "https://blog.kuangstudy.com/", "2890850899@qq.com");
        return new ApiInfo(
                "饶广",    //标题
                "nihaoya",   //描述
                "v1.0",   //版本
                "https://blog.kuangstudy.com/",   //组织名或者作者信息或者博客网址
                contact,
                "Apache 2.0",
                "http:www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

    ;

}
